package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.enums.Constant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.V2.inter.mapper.*;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.model.service.AsyncService;
import com.ruoyi.system.outto.service.IOutApi;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.MapUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {


    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Resource
    private JymdataMapper jymdataMapper;

    @Autowired
    private AsyncService asyncService;




    /**
     * 盼之接口刷新
     */
    @Scheduled(cron = "0 0 17 * * ?")
    public void refPZZ(){

        JSONObject req = new JSONObject();
        req.put("pageSize",10);
        JSONObject action = new JSONObject();
        action.put("status","ON_STAND");
        req.put("action",action);
        String url = "https://www.pzds.com/api/v2/userCenter/pageGoods";

        String token = "";
        JSONObject reqtoken = new JSONObject();
        JSONObject actiontoken = new JSONObject();
        actiontoken.put("phone","15173678868");
        String sign = DigestUtils.md5Hex("yangj126");
        actiontoken.put("password",sign);
        reqtoken.put("action",actiontoken);

        Map<String,String> head = new HashMap<>();
        head.put("content-type","application/json");

        String tokenUrl = "https://www.pzds.com/api/v2/user/public/login/password";
        try {
            String tokenres = HttpUtils.sendPost(tokenUrl, JSONObject.toJSONString(reqtoken), head);
            JSONObject jsonObject = JSONObject.parseObject(tokenres);
            token = jsonObject.getString("data");
        } catch (IOException e) {
            e.printStackTrace();
        }
        head.put("token",token);

        Integer page = 0;
        boolean flag = true;
        while (flag){
            page++;
            req.put("page",page);
            String res = "";
            try {
                res = HttpUtils.sendPost(url, JSONObject.toJSONString(req), head);
            } catch (IOException e) {
                e.printStackTrace();
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            JSONObject data = jsonObject.getJSONObject("data");
            Integer totalPages = data.getInteger("totalPages");
            if (page == totalPages){
                flag = false;
            }
            if (totalPages > 0){
                String records = data.getString("records");
                List<JSONObject> jsonObjects = JSONObject.parseArray(records, JSONObject.class);
                if (jsonObjects.size() < 10){
                    flag = false;
                }
                for (JSONObject object : jsonObjects) {
                    String goodsNo = object.getString("goodsNo");
                    try {

                        String detailsUrl = "https://www.pzds.com/api/v2/userCenter/addGoodsTempHeat";
                        JSONObject detailsQeq = new JSONObject();
                        JSONObject actionDetails = new JSONObject();
                        actionDetails.put("goodsNo",goodsNo);
                        detailsQeq.put("action",actionDetails);
                        String  detaisStr = HttpUtils.sendPost(detailsUrl, JSONObject.toJSONString(detailsQeq), head);
                        System.out.println(detaisStr);
                        Thread.sleep(2000l);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


//    //3.添加定时任务
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void setZhuanzhuandzys() {
//        //转转数据
//        commonZhuanzhuan(19522,"原神");
//        commonZhuanzhuan(10874,"王者荣耀");
//        commonZhuanzhuan(10877,"和平精英");
//        commonZhuanzhuan(72008,"蛋仔派对");
//        commonZhuanzhuan(10492,"英雄联盟");
//
//        //交易猫数据
//        String url = "https://m.jiaoyimao.com/jg1002600/f2043922-c2043923/o110/?rId=107";
//        commonPushData(url,"穿越火线手游");
//
//        url = "https://m.jiaoyimao.com/jg1009609/f1856397-c1856398/o110/?rId=107";
//        commonPushData(url,"原神");
//
//        url = "https://m.jiaoyimao.com/jg1005668-5/f2103394-c2103395/o110/?rId=107";
//        commonPushData(url,"蛋仔派对");
//
//        url = "https://m.jiaoyimao.com/jg1005668-5/f2103394-c2103395/o110/?rId=107";
//        commonPushData(url,"CF端游");
//
//        url = "https://m.jiaoyimao.com/jg1005654-5/f2103139-c2103140/o110/?rId=107";
//        commonPushData(url,"英雄联盟");
//
//        url = "https://m.jiaoyimao.com/jg1006473/f2125467-c2125468/o110/?rId=107";
//        commonPushData(url,"和平精英");
//
//        url = "https://m.jiaoyimao.com/jg1002416/f1724247-c1724248/o110/?rId=107&enforcePlat=2&spm=gcmall.goods_listpage.0.0";
//        commonPushData(url,"王者荣耀");
//    }
//
//    //3.添加定时任务
//    @Scheduled(cron = "0/10 * * * * ?")
//    public void delJymData() {
//        jymdataMapper.deleteJymdataHour();
//    }
//
//    private void commonPushData(String url,String type){
//     Connection connect = Jsoup.connect(url);
//        try {
//            Document document = connect.get();
//            String title = document.title();
//            System.out.println(title);
//            Elements elementsByClass = document.getElementsByClass("page-content ");
//            Element element = elementsByClass.get(1);
//            Elements goodList = element.getElementsByClass("goods-list");
//            for (Element goodE : goodList) {
//                Elements goodDetails = goodE.getElementsByClass("goodsList-item goldexp goldclk");
//                if (!CollectionUtils.isEmpty(goodDetails)){
//                    commonValue(goodDetails,type);
//                }else {
//                    goodDetails = goodE.getElementsByClass("goodsList-item goldexp goldclk ");
//                    if (!CollectionUtils.isEmpty(goodDetails)){
//                        commonValue(goodDetails,type);
//                    }
//                    if (CollectionUtils.isEmpty(goodDetails)){
//                        goodDetails = goodE.getElementsByClass("goodsList-item-v2 goldexp goldclk ");
//                        commonValue(goodDetails,type);
//                    }
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    //3.添加定时任务
////    @Scheduled(cron = "0 0/1 * * * ?")
//    public void setZhuanzhuandzcfm() {
//        commonZhuanzhuan(10875,"穿越火线手游");
//    }
//
//
//    private void commonZhuanzhuan(Integer type,String typeName){
//        String URL = "https://app.zhuanzhuan.com/zzopen/gameAccount/accountRegionInfoList";
//
//        Map<String,Object> parsm = new HashMap<>();
//        parsm.put("pageSize",20);
//        parsm.put("pageNo",1);
//        parsm.put("isKind","true");
//        parsm.put("productType",2123001);
//        parsm.put("sysType",1);
////        parsm.put("clientType",123001);
//        parsm.put("pgCateId",2123001);
////        parsm.put("pgBrandId",10874); 王者
////        parsm.put("pgBrandId",10877); //和平
////        parsm.put("pgBrandId",19522); //原神
//        parsm.put("pgBrandId",type); //cf手游
//        parsm.put("pgModelId",0);
//        parsm.put("queryOfficial",10);
//        parsm.put("metSource","game--account_list--kingkong--hot-game-dynamic");
//        parsm.put("sellerGuaranteePermanent","0");
//        parsm.put("fullText","");
//        parsm.put("publishTime","0");
//        parsm.put("sortType","2");
//
//        String tokenres = HttpUtils.doPostForm(URL, parsm, null);
//
//        System.out.println(tokenres);
//        JSONObject jsonObject = JSONObject.parseObject(tokenres);
//        if (Objects.equals(jsonObject.getString("respCode"),"0")){
//            JSONObject respData = jsonObject.getJSONObject("respData");
//            String productList = respData.getString("productList");
//            List<JSONObject> jsonObjects = JSONObject.parseArray(productList, JSONObject.class);
//            for (JSONObject object : jsonObjects) {
//
//                String content = object.getString("content");
//                String shopurl = "https://m.zhuanzhuan.com/platform/game_business/index.html?webview=zzn&init_from=2_5_14357_0&metric=mWrP8Y0I3N4W0zNVXVX7OQf406ayi&infoId=accouid&needHideHead=3&fromChannel=kingkong&b2cProduct=0&c2cVersion=c2c_new_v2&__zpm=1%5EO5742%5E8%5E0%5E0%7C3%5EW4951%5E203%5E3%5E0&tt=9F315A336A1E3A2789B24CF7690301A31680782957230&zzv=9.9.0&zzfrom=LinkCopy#/account/detail";
////                accouid
//                String productId = object.getString("productId");
//                shopurl = shopurl.replace("accouid",productId);
//                Jymdata jymdata = new Jymdata();
//                content = content.replace("\uD83D\uDC51","").replace("\uD83C\uDF4E","");
//                jymdata.setDes(content);
//                jymdata.setShopurl(shopurl);
//                jymdata.setRecid(productId);
//                jymdata.setShopid(productId);
//                jymdata.setChannel("转转");
//                jymdata.setGame(typeName);
//
//                Integer nowPrice = object.getInteger("nowPrice");
//                if (Objects.nonNull(nowPrice)){
//                    jymdata.setPrice(String.valueOf(nowPrice/100));
//                }
//
//                Jymdata jymd = jymdataMapper.selectJymdataByShopid(productId);
//                if (Objects.isNull(jymd)){
//                    jymdataMapper.insertJymdata(jymdata);
//                }
//            }
//        }
//    }
//
//    private void commonValue( Elements goodDetails,String type){
//        for (Element first : goodDetails) {
//
//            Jymdata jymdata = new Jymdata();
//
//            String attr = first.select("a").attr("href");
//            jymdata.setShopurl(attr);
//            String goodsid = first.select("a").attr("data-goodsid");
//            jymdata.setShopid(goodsid);
//            String goodsName = first.select("a").attr("data-goods-name");
//            jymdata.setDes(goodsName);
//            String recid = first.select("a").attr("data-recid");
//            jymdata.setRecid(recid);
//            String publisher = first.select("a").attr("data-publisher");
//            jymdata.setRegional(publisher);
//            jymdata.setChannel("交易猫");
//
//            boolean b = true;
//            int i = 15;
//            while (b){
//                Elements select = first.getElementsByClass("integer-part price-"+i);
//                if (!CollectionUtils.isEmpty(select)){
//                    b = false;
//                    Element f = select.first();
//                    String text = f.text();
//                    jymdata.setPrice(text);
//                }
//                if (i > 25){
//                    b = false;
//                }
//                i++;
//            }
//            jymdata.setGame(type);
//            Jymdata data = jymdataMapper.selectJymdataByShopid(goodsid);
//            if (Objects.isNull(data )){
//                jymdataMapper.insertJymdata(jymdata);
//            }
//        }
//    }


//    @Scheduled(cron = "0 0 3 * * ?")
//    public void delAccount() {
//
//        GoodsInfo goodsInfo = new GoodsInfo();
//
//        goodsInfo.setSaleState(3);
//        List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectGoodsInfoList(goodsInfo);
//        for (GoodsInfo info : goodsInfoList) {
//            asyncService.excuTaskPool(info.getId(), null, Constant.del, goodsInfo.getCreateId(), 0l);
//        }
//    }
//    @Scheduled(cron = "0 0 3 * * ?")
//    public void delAccountS() {
//
//        GoodsInfo goodsInfo = new GoodsInfo();
//
//        goodsInfo.setSaleState(4);
//        List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectGoodsInfoList(goodsInfo);
//        for (GoodsInfo info : goodsInfoList) {
//            asyncService.excuTaskPool(info.getId(), null, Constant.del, goodsInfo.getCreateId(), 0l);
//        }
//    }





}